package mlta.activities;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class CoursesTopicActivity extends Activity {

	protected Button btn_help;

	protected Button btn_exit;

	protected Button btn_home;

	protected Button btn_refresh;

	protected Button btn_search;

	protected Button btn_new;

	protected Button btn_old;

	protected Button btn_back;
	
	protected long course_id = -1;
	
	protected String username;
	
	protected boolean novo = false;
	
	protected String titulo;
	
	protected String message;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.topics);

		btn_help = (Button) findViewById(R.id.topics_help);
		btn_exit = (Button) findViewById(R.id.topics_exit);
		btn_home = (Button) findViewById(R.id.topics_home);
		btn_refresh = (Button) findViewById(R.id.topics_refresh);
		btn_search = (Button) findViewById(R.id.topics_search);
		btn_new = (Button) findViewById(R.id.topics_new);
		btn_old = (Button) findViewById(R.id.topics_old);
		btn_back = (Button) findViewById(R.id.topics_back);
		
		course_id = getIntent().getLongExtra("id", -1);
		username = getIntent().getStringExtra("username");
		novo = getIntent().getBooleanExtra("novo", novo);
		titulo = getIntent().getStringExtra("titulo");
		message = getIntent().getStringExtra("message");

		// evento botao help (?)
		btn_help.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				Toast.makeText(getApplicationContext(),
						"Feature not ready yet!!!", Toast.LENGTH_LONG).show();
			}

		});

		// evento botao exit
		btn_exit.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				System.exit(0);
			}

		});

		// evento botao home
		btn_home.setOnClickListener(new View.OnClickListener() {

			// Vai pra qual tela a sua ??
			public void onClick(View arg0) {
				Intent intent = new Intent(CoursesTopicActivity.this, CoursesActivity.class);
				CoursesTopicActivity.this.startActivity(intent);
				intent.putExtra("username", username);
				System.exit(0);
			}

		});

		// evento botao atualizar
		btn_refresh.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				Intent intent = new Intent(CoursesTopicActivity.this, CoursesTopicActivity.class);
				intent.putExtra("id", getIntent().getLongExtra("id", -1));
				intent.putExtra("username", username);
				intent.putExtra("novo", novo);
				intent.putExtra("titulo", titulo);
				intent.putExtra("message", message);
				CoursesTopicActivity.this.startActivity(intent);
				System.exit(0);
			}

		});

		// evento botao procurar/pesquisar
		btn_search.setOnClickListener(new View.OnClickListener() {

			// Vai pra tela da sofia
			public void onClick(View arg0) {
				Intent intent = new Intent(CoursesTopicActivity.this, SearchActivity.class);
				intent.putExtra("course_id", course_id);
				intent.putExtra("username", username);
				intent.putExtra("novo", novo);
				intent.putExtra("titulo", titulo);
				intent.putExtra("message", message);
				intent.putExtra("tipo", 2);
				CoursesTopicActivity.this.startActivity(intent);
				System.exit(0); 
			}

		});

		// evento botao novo
		btn_new.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				Intent intent = new Intent(CoursesTopicActivity.this, NewTopicActivity.class);
				intent.putExtra("course_id", course_id);
				intent.putExtra("username", username);
				intent.putExtra("novo", novo);
				intent.putExtra("titulo", titulo);
				intent.putExtra("message", message);
				CoursesTopicActivity.this.startActivity(intent);
				System.exit(0);
			}

		});

		// evento botao old
		btn_old.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				Toast.makeText(getApplicationContext(), "No more topics!",
						Toast.LENGTH_LONG).show();
			}

		});

		// evento botao voltar
		btn_back.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				Intent intent = new Intent(CoursesTopicActivity.this, CoursesActivity.class);
				intent.putExtra("username", username);
				CoursesTopicActivity.this.startActivity(intent);
				System.exit(0);
			}

		});

		ListView listView = (ListView) findViewById(R.id.topics_list);

		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
			
		if (course_id == 0) {

			HashMap<String, String> map = new HashMap<String, String>();
			map.put("Topic", "All");
			if(novo)
				map.put("unread", "(9)");
			else
				map.put("unread", "(8)");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("Topic", "Data da Prova");
			map.put("unread", "(5)");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("Topic", "Parte 1 do Projeto");
			map.put("unread", "(1)");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("Topic", "Horario: Aula 15/05");
			map.put("unread", "(1)");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("Topic", "Dúvidas sobre a Parte 1 do Projeto");
			map.put("unread", "(1)");
			list.add(map);

		} else {

			HashMap<String, String> map = new HashMap<String, String>();
			map.put("Topic", "All");
			if(novo)
				map.put("unread", "(1)");
			else
				map.put("unread", "(0)");
			list.add(map);
			
		}
		
		if(novo) {
			HashMap<String, String> newMap = new HashMap<String, String>();
			newMap.put("Topic", titulo);
			newMap.put("unread", "(1)");
			list.add(newMap);
		}

		SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.topic,
				new String[] { "Topic", "unread" }, new int[] { R.id.label,
						R.id.count });

		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				Intent intent = new Intent(CoursesTopicActivity.this, CommentsActivity.class);
				intent.putExtra("topic_id", id);
				intent.putExtra("course_id", course_id);
				intent.putExtra("username", username);
				intent.putExtra("novo", novo);
				intent.putExtra("titulo", titulo);
				intent.putExtra("message", message);
				CoursesTopicActivity.this.startActivity(intent);
				System.exit(0);
			}

		});

	}
	
	@Override
    public void onBackPressed() {
		Intent intent = new Intent(CoursesTopicActivity.this,
				CoursesActivity.class);
		intent.putExtra("username", username);
		CoursesTopicActivity.this.startActivity(intent);
		System.exit(0);
    } 
	
}
